package com.meme.io.helper;

import java.io.RandomAccessFile;

import com.meme.util.FileUtil;

public abstract class CodeFileWriter extends RandomAccessFile
{
	public CodeFileWriter (String filename, String fileExt) throws Exception
	{
		super ( preHandle (filename, fileExt), "rw" );
	}
	
	static String preHandle (String filename, String fileExt)
	{
		String ext = FileUtil.getFilenameExt (filename);
		if ( !ext.equals(fileExt) )
			filename = filename + fileExt;
		
		FileUtil.deleteFile (filename);
		
		return filename;
	}
	
	int mIndents = 3;
	public void setIndents (int indents)
	{
		mIndents = indents;
	}
	
	public int getIndents ()
	{
		return mIndents;
	}
	
	int mTotalIndentSpaces = 0;
	public void setCurrentIndents (int nSpaces)
	{
		if (nSpaces < 0)
			nSpaces = 0;
		mTotalIndentSpaces = nSpaces;
	}
	
	public int getCurrentIndents ()
	{
		return mTotalIndentSpaces;
	}
	
	public void incIndents ()
	{
		setCurrentIndents (mTotalIndentSpaces + mIndents);
	}
	
	public void decIndents ()
	{
		setCurrentIndents (mTotalIndentSpaces - mIndents);
	}
	
	public void writeIndentSpaces () throws Exception
	{
		for (int i = 0; i < mTotalIndentSpaces; ++ i)
			writeByte (' ');
	}
	
	public void writeTextLine (String text) throws Exception
	{
		writeTextLine (text, true);
	}
	
	public void writeTextLine (String text, boolean writeIndentSpaces) throws Exception
    {
    	writeText (text, writeIndentSpaces);
    	writeNewLine ();
    }
    
	public void writeText (String texts) throws Exception
    {
    	writeText (texts, false);
    }
    
	public void writeText (String text, boolean writeIndentSpaces) throws Exception
    {
    	if (writeIndentSpaces)
    		writeIndentSpaces ();
        writeBytes (text);
    }

	public void writeNewLine () throws Exception
    {
    	writeNewLines (1);
    }
    
	public void writeNewLines (int lines) throws Exception
    {
        while ( -- lines >= 0 ) writeBytes ("\r\n");
    }
	
	public abstract void writeAssignment (String varName, String varType, String varValue, boolean bFinal, boolean bStatic) throws Exception;

	abstract String getBooleanString ();
	abstract String getIntString ();
	abstract String getFloatString ();
	abstract String getStringString ();
	
    public abstract void writePackageHead (String packageName) throws Exception;
    public abstract void writePackageTail (String packageName) throws Exception;
    public abstract void writeClassHead (String className) throws Exception;
    public abstract void writeClassTail (String className) throws Exception;
    

	
	public void writeConstBooleanVariable (String varName, boolean varValue) throws Exception
	{
		writeIndentSpaces ();
		writeText ("public ");
		writeAssignment (varName, getBooleanString (), varValue ? "true" : "false", true, true );
		writeBytes ("\r\n");
	}
	
	public void writeConstIntegerVariable (String varName, int varValue) throws Exception
	{
		writeConstIntegerVariable (varName, "" + varValue );
	}
	
	public void writeConstIntegerVariable (String varName, String varValue) throws Exception
	{
		writeIndentSpaces ();
		writeText ("public ");
		writeAssignment (varName, getIntString (), varValue, true, true );
		writeBytes ("\r\n");
	}
	
	public void writeConstHexIntegerVariable (String varName, int varValue) throws Exception
	{
		writeConstIntegerVariable (varName, "0x" + Integer.toHexString(varValue) );
	}
	
	public void writeConstFloatVariable (String varName, float varValue) throws Exception
	{
		writeIndentSpaces ();
		writeText ("public ");
		writeAssignment (varName, getFloatString (), "" + varValue, true, true );
		writeBytes ("\r\n");
	}
	
	public void writeConstStringVariable (String varName, String varValue) throws Exception
	{
		writeIndentSpaces ();
		writeText ("public ");
		writeAssignment (varName, getBooleanString (), varValue, true, true );
		writeBytes ("\r\n");
	}
}
